#include <ctime>
#include <iostream>
#include <string>
#include <array>
#include <boost/asio.hpp>

using boost::asio::ip::udp;
using namespace std;
using namespace boost::asio;

string make_daytime_string()
{
    time_t now = time(0);
    return ctime(&now);
}

int main(int argc, char const *argv[])
{
    try
    {
        io_service  io_service;
        udp::socket socket(io_service, udp::endpoint(udp::v4(), 12345));

        for (;;)
        {
            array<char, 1>            recv_buf;
            udp::endpoint             remote_endpoint;
            boost::system::error_code error;
            socket.receive_from(buffer(recv_buf), remote_endpoint, 0, error);

            if (error && error != error::message_size)
                throw boost::system::system_error(error);

            string message = make_daytime_string();

            boost::system::error_code ignored_error;
            socket.send_to(buffer(message), remote_endpoint, 0, ignored_error);
        }
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }

    return 0;
}
